<template>
	<view class="wxLogin-content">
		<view class="login-logo">
			<image :src="logo"></image>
		</view>
		<view class="quickLogin">
			<button class="loginBtn" type="primary" @click="quickLogin">微信一键登录</button>
			<text class="tip">未注册的账号验证通过后将自动注册</text>
		</view>

	</view>
</template>

<script setup>
import { login } from '../../api/user';

// weixin login
const quickLogin = (e) => {
	uni.showModal({
		title: '微信一键登录',
		content: '是否授权微信登录？',
		confirmText: '确认登录',
		cancelText: '取消',
		success: function (res) {
			if (res.confirm) {
				// 用户确认后执行登录
				weixinLogin();
			} else if (res.cancel) {
				uni.showToast({
					title: '已取消登录',
					icon: 'none'
				});
			}
		}
	});
}

const weixinLogin = (res) => {

	uni.login({
		provider: 'weixin',
		success: ({code}) => {
			// 发送请求到后端进行登录
			login({
				code: code
			}).then((res) => {
				console.log('登录成功',res);
			}).catch((err) => {
				console.log('登录失败', err);
			})

		}
	})
}

</script>

<style lang="scss" scoped>
.quickLogin {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;

	.loginBtn {
		width: 95%;
		border-radius: 30rpx;
	}

	.tip {
		margin-top: 20rpx;
		font-size: 28rpx;
		color: #999999;
	}
}
</style>